﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraGauges.Win.Gauges.State;
using Zero.POCO.Donnees;
using Zero.POCO.Views;

namespace DevExpress.MailClient.Win.Controls.PartAudience {
    public partial class WidgetEvolutionPartAudience : UserControl {
        Timer updateTimer = new Timer();
        static Color positiveColor = Color.FromArgb(70, 158, 165);
        static Color negativeColor = Color.FromArgb(165, 70, 113);
        public WidgetEvolutionPartAudience()
        {
            InitializeComponent();
            updateTimer.Interval = 1000;
            updateTimer.Tick += OnTimerTick;
            updateTimer.Start();
            OnTimerTick(this, EventArgs.Empty);
        }
        public void UpdateStat(PartAudienceMain partAudienceMain)
        {

            cEvolutionPartAudience.Series.Clear();


            var series1 = new Series(partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.SupportView.Nom, ViewType.Line);
            
            List<int> listVague = partAudienceMain.VaguesRef.Select(e => e.Vague.Mois).ToList();
            listVague.Sort();
            for (int i = 0; i < listVague.Count; i++)
            {
                var profilPenetrationSup = partAudienceMain.VaguesRef.First(e => e.Vague.Mois == listVague[i]);
                if (profilPenetrationSup.ProfilSupportPricncipale.PenetrationTotal == 0) 
                    series1.Points.Add(new SeriesPoint(profilPenetrationSup.Vague.Libelle, 0));
                else
                    series1.Points.Add(new SeriesPoint(profilPenetrationSup.Vague.Libelle, profilPenetrationSup.ProfilSupportPricncipale.Penetration * 100 / profilPenetrationSup.ProfilSupportPricncipale.PenetrationTotal));
            }
            cEvolutionPartAudience.Series.Add(series1);

            foreach (var support in partAudienceMain.VaguePrincipale.ProfilSupport)
            {
                var series2 = new Series(support.SupportView.Nom, ViewType.Line);

                listVague.Sort();
                for (int i = 0; i < listVague.Count; i++)
                {
                    var profilPenetrationSup = partAudienceMain.VaguesRef.First(e => e.Vague.Mois == listVague[i]);
                    if (support.PenetrationTotal == 0)
                        series2.Points.Add(new SeriesPoint(profilPenetrationSup.Vague.Libelle, 0));
                    else
                        series2.Points.Add(new SeriesPoint(profilPenetrationSup.Vague.Libelle, profilPenetrationSup.ProfilSupport.First(e => e.SupportView.Id == support.SupportView.Id).Penetration * 100 / profilPenetrationSup.ProfilSupport.First(e => e.SupportView.Id == support.SupportView.Id).PenetrationTotal));
                }
                series2.Points.Add(new SeriesPoint(partAudienceMain.VaguePrincipale.Vague, support.Penetration * 100 / support.PenetrationTotal));
                cEvolutionPartAudience.Series.Add(series2);
            }
            /*foreach (var source in partAudienceMain.VaguesRef.Where(e=>e.ProfilSupportPricncipale.))
            {
                
            }
            /*ccPartAudience.Series.Clear();
            var series1 = new Series(partAudienceMain.VaguePrincipale.Vague.Libelle, ViewType.Doughnut);
            float sum = 0;
            sum = sum + partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.Penetration;
            series1.Points.Add(new SeriesPoint(partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.SupportView.Nom, partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.Penetration * 100 / partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.PenetrationTotal));
            foreach (var profilSupport in partAudienceMain.VaguePrincipale.ProfilSupport)
            {

                series1.Points.Add(new SeriesPoint(profilSupport.SupportView.Nom, profilSupport.Penetration * 100 / profilSupport.PenetrationTotal));
                sum = sum + profilSupport.Penetration;
            }
            if (partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.PenetrationTotal - sum > 0)
                series1.Points.Add(new SeriesPoint("Autres Chaines", (partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.PenetrationTotal - sum) * 100 / partAudienceMain.VaguePrincipale.ProfilSupportPricncipale.PenetrationTotal));

            ccPartAudience.Series.Add(series1);




            // Format the the series labels.
            series1.Label.TextPattern = "{A}: {VP:p0}";*/
        }


        int index = 0;
       // StockData previousData;
        void OnTimerTick(object sender, EventArgs e) {
          /*  if(IsDisposed) { updateTimer.Stop(); return; }
            StockData data = StockDataCreator.GetData(index);
            if(index > 0) {
                decimal d = data.ClosePrice - previousData.ClosePrice;
                decimal dq = (d * 100) / data.ClosePrice;
                labelControl5.Text = Math.Round( d, 2, MidpointRounding.ToEven).ToString().TrimEnd('0');
                stateIndicatorComponent1.StateIndex =  previousData.ClosePrice - data.ClosePrice < 0 ? 0 : 1;
                if(stateIndicatorComponent1.StateIndex == 0) {
                    labelControl5.ForeColor = positiveColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=70,158,165>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
                else {
                    labelControl5.ForeColor = negativeColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=165,70,113>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
            }
            previousData = data;
            index++;*/
        }
        private void OnCustomDrawElement(object sender, DevExpress.XtraGauges.Core.Primitive.CustomDrawElementEventArgs e) {
            e.Handled = true;
            StateIndicatorComponent indicator = sender as StateIndicatorComponent;
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;
            Point p3 = Point.Empty;
            int height = (int)(e.Info.BoundBox.Width * Math.Sqrt(3) / 2);
            p1.X = (int)(e.Info.BoundBox.Width / 2);
            p2.X = (int)e.Info.BoundBox.Left;
            p3.X = (int)e.Info.BoundBox.Right;
            int y = (int)((e.Info.BoundBox.Height - height) / 2);
            Color stateColor = Color.Empty;
            if(indicator.StateIndex == 0) {
                p1.Y = y;
                p2.Y = p3.Y = y +height;
                stateColor = positiveColor;
            }
            else {
                p1.Y = y + height;
                p2.Y = p3.Y = y;
                stateColor = negativeColor;
            }
            e.Context.Graphics.FillPolygon(new SolidBrush(stateColor), new Point[] { p1, p2, p3 });
        }
    }
}
